<!-- 补贴申请 -->
<template>
  <div class="subsidy">
    <!-- subsidy-header -->
    <div class="subsidy-header">
      <span>补贴申请</span>
    </div>
    <!-- subsidy-header -->

    <!-- subsidy-inform -->
    <div class="subsidy-inform">
      <el-table :data="tableData" style="width: 100%" stripe height="300">
        <!-- 下拉框 -->
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="工号">
                <span v-if="props.row.show">
                  <el-input
                    size="mini"
                    style="width: 100%"
                    placeholder="请输入工号"
                    v-model="props.row.id"
                  ></el-input>
                </span>
                <span v-else>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="姓名">
                <span v-if="props.row.show">
                  <el-input
                    size="mini"
                    style="width: 100%"
                    placeholder="请输入姓名"
                    v-model="props.row.name"
                  ></el-input>
                </span>
                <span v-else>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="补贴类型">
                <span v-if="props.row.show">
                  <el-select
                    style="width: 100%"
                    v-model="props.row.type"
                    placeholder="请选择申请类型"
                  >
                    <el-option label="话费补贴" value="话费补贴"></el-option>
                    <el-option label="住房补贴" value="住房补贴"></el-option>
                    <el-option label="出差补贴" value="出差补贴"></el-option>
                    <el-option label="餐食补贴" value="餐食补贴"></el-option>
                  </el-select>
                </span>
                <span v-else>{{ props.row.type }}</span>
              </el-form-item>
              <el-form-item label="部门经理">
                <span v-if="props.row.show">
                  <el-input
                    size="mini"
                    style="width: 100%"
                    placeholder="请输入部门经理"
                    v-model="props.row.manager"
                  ></el-input>
                </span>
                <span v-else>{{ props.row.manager }}</span>
              </el-form-item>
              <el-form-item label="申请时间">
                <span v-if="props.row.show">
                  <el-date-picker
                    type="date"
                    placeholder="选择日期"
                    v-model="ruleForm.date1"
                    style="width: 100%"
                  ></el-date-picker>
                </span>
                <span v-else>{{ props.row.date1 }}</span>
              </el-form-item>
              <el-form-item label="申请原因">
                <span v-if="props.row.show">
                  <el-input
                    style="width: 100%"
                    type="textarea"
                    v-model="ruleForm.reason"
                  ></el-input>
                </span>
                <span v-else style="display: inline-block; width: 100%">{{
                  props.row.id
                }}</span>
              </el-form-item>
              <el-form-item label="入职日期">
                <span v-if="props.row.show">
                  <el-date-picker
                    type="date"
                    placeholder="选择日期"
                    v-model="ruleForm.date2"
                    style="width: 100%"
                  ></el-date-picker>
                </span>
                <span v-else>{{ props.row.date2 }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <!-- 下拉框 -->

        <el-table-column label="工号" prop="id"> </el-table-column>
        <el-table-column label="姓名" prop="name"> </el-table-column>
        <el-table-column label="申请类型" prop="type"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              plain
              @click="handleEdit(scope.$index, scope.row)"
              >{{ scope.row.show ? "保存" : "编辑" }}</el-button
            >
            <el-button
              size="mini"
              type="danger"
              plain
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- subsidy-inform 显示信息-->

    <!-- subsidy-Application 填写申请表单 -->
    <div class="subsidy-Application">
      <span @click="centerDialogVisible = true">
        <el-button type="success" icon="el-icon-edit" plain
          >填写申请表</el-button
        >
      </span>

      <!-- 弹框 -->
      <div class="hr-personnel-dialog">
        <el-dialog
          title="填写申请表"
          :visible.sync="centerDialogVisible"
          center
        >
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="工号" prop="id">
              <el-input v-model="ruleForm.id" style="width: 60%"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
              <el-input v-model="ruleForm.name" style="width: 60%"></el-input>
            </el-form-item>
            <el-form-item label="部门经理" prop="manager">
              <el-input
                v-model="ruleForm.manager"
                style="width: 60%"
              ></el-input>
            </el-form-item>
            <el-form-item label="补贴类型" prop="type">
              <el-select v-model="ruleForm.type" placeholder="请选择申请类型">
                <el-option label="话费补贴" value="话费补贴"></el-option>
                <el-option label="住房补贴" value="住房补贴"></el-option>
                <el-option label="出差补贴" value="出差补贴"></el-option>
                <el-option label="餐食补贴" value="餐食补贴"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="申请日期" required>
              <el-col :span="11">
                <el-form-item prop="date1">
                  <el-date-picker
                    type="date"
                    placeholder="选择日期"
                    v-model="ruleForm.date1"
                    style="width: 100%"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item label="入职时间" required>
              <el-col :span="11">
                <el-form-item prop="date2">
                  <el-date-picker
                    type="date"
                    placeholder="选择日期"
                    v-model="ruleForm.date2"
                    style="width: 100%"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item label="申请原因" prop="reason">
              <el-input type="textarea" v-model="ruleForm.reason"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm(ruleForm)">提交申请</el-button>
              <el-button  @click="resetForm('ruleForm')">重置</el-button>
              <!-- <el-button @click="cancel('ruleForm')">取消</el-button> -->
            </el-form-item>
          </el-form>
        </el-dialog>
      </div>
      <!-- 弹框 -->

      <!-- <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="工号" prop="id">
          <el-input v-model="ruleForm.id" style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="ruleForm.name" style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="部门经理" prop="manager">
          <el-input v-model="ruleForm.manager" style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item label="补贴类型" prop="type">
          <el-select v-model="ruleForm.type" placeholder="请选择申请类型">
            <el-option label="话费补贴" value="话费补贴"></el-option>
            <el-option label="住房补贴" value="住房补贴"></el-option>
            <el-option label="出差补贴" value="出差补贴"></el-option>
            <el-option label="餐食补贴" value="餐食补贴"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请日期" required>
          <el-col :span="11">
            <el-form-item prop="date1">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="ruleForm.date1"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="入职时间" required>
          <el-col :span="11">
            <el-form-item prop="date2">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="ruleForm.date2"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="申请原因" prop="reason">
          <el-input type="textarea" v-model="ruleForm.reason"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleForm)"
            >提交申请</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form> -->
      <!-- subsidy-Application 填写申请表单 -->
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      tableData: [],
      formshow: false,
      dialogVisible: false,
      centerDialogVisible: false,
      ruleForm: {
        id: "",
        name: "",
        manager: "",
        type: "",
        date1: "",
        date2: "",
        reason: "",
        show: false,
      },
      rules: {
        id: [
          { required: true, message: "请输入工号", trigger: "blur" },
          {
            min: 1,
            max: 15,
            message: "长度在 1 到 15 个字符",
            trigger: "blur",
          },
        ],
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        manager: [
          { required: true, message: "请输入部门经理", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        region: [
          { required: true, message: "请选择补贴类型", trigger: "change" },
        ],
        date1: [
          {
            type: "date",
            required: true,
            message: "请选择申请日期",
            trigger: "change",
          },
        ],
        date2: [
          {
            type: "date",
            required: true,
            message: "请选择入职日期",
            trigger: "change",
          },
        ],
        reason: [
          { required: true, message: "请填写申请补贴的原因", trigger: "blur" },
        ],
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    // 获取本地数据 getInform
    getInform: function () {
      var list = localStorage.getItem("tableData") || "[]";
      return JSON.parse(list);
    },

    // 将数据存回本地
    setInform: function (arr) {
      localStorage.setItem("tableData", JSON.stringify(arr));
    },

    // 点击添加申请
    submitForm: function (res) {
      if (
        this.ruleForm.id != "" &&
        this.ruleForm.name != "" &&
        this.ruleForm.manager != "" &&
        this.ruleForm.type != "" &&
        this.ruleForm.date1 != "" &&
        this.ruleForm.date2 != "" &&
        this.ruleForm.reason != "" 
      ) {
        console.log(res);
        let yy1 = String(new Date(res.date1).getFullYear());
        let mm1 = new Date(res.date1).getMonth() + 1;
        let dd1 = new Date(res.date1).getDate();
        var date1 = yy1 + "-" + mm1 + "-" + dd1;
        // console.log(date1)

        let yy2 = String(new Date(res.date2).getFullYear());
        let mm2 = new Date(res.date2).getMonth() + 1;
        let dd2 = new Date(res.date2).getDate();
        var date2 = yy2 + "-" + mm2 + "-" + dd2;
        // console.log(date2)
        var newInform = {
          id: res.id,
          name: res.name,
          type: res.type,
          review: res.review,
          manager: res.manager,
          date1: date1,
          date2: date2,
          reason: res.reason,
          show: res.show,
        };

        // 获取申请信息
        var informs = this.getInform();

        // 添加新信息
        informs.push(newInform);
        // console.log(informs)

        // 将数组再次传回 local Storage 中
        this.setInform(informs);

        // 自动刷新
        this.$router.go(0);
      } else {
        alert("请完善填写，谢谢您的配合！")
      }
    },

    // 点击编辑/保存
    handleEdit(index, row) {
      console.log(index, row);
      console.log(row.show);
      row.show = row.show ? false : true;
      console.log(row.show);

      // 保存
      if (row.show == false) {
        let yy1 = String(new Date(row.date1).getFullYear());
        let mm1 = new Date(row.date1).getMonth() + 1;
        let dd1 = new Date(row.date1).getDate();
        var date1 = yy1 + "-" + mm1 + "-" + dd1;
        // console.log(date1)

        let yy2 = String(new Date(row.date2).getFullYear());
        let mm2 = new Date(row.date2).getMonth() + 1;
        let dd2 = new Date(row.date2).getDate();
        var date2 = yy2 + "-" + mm2 + "-" + dd2;
        // console.log(date2)
        var newInform = {
          id: row.id,
          name: row.name,
          type: row.type,
          review: row.review,
          manager: row.manager,
          date1: date1,
          date2: date2,
          reason: row.reason,
          show: row.show,
        };

        // 获取申请信息
        var informs = this.getInform();

        // 添加新信息
        informs.splice(index, 1, newInform);
        // console.log(informs)

        // 将数组再次传回 local Storage 中
        this.setInform(informs);

        // 自动刷新
        // this.$router.go(0);
      }
    },

    // 删除节点
    handleDelete(index, row) {
      console.log(index, row);
      // 删除点击的节点的数据
      let list = this.getInform();
      console.log(list);
      list.splice(index, 1);
      this.setInform(list);

      // 自动刷新
      this.$router.go(0);
    },

    // 点击重置
    resetForm: function () {
      this.ruleForm = {
        id: "",
        name: "",
        manager: "",
        type: "",
        date1: "",
        date2: "",
        reason: "",
      };
    },

    // 取消
    cancel: function (){

    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.tableData = this.getInform();
    // this.tabledata.map((i) => {
    //   i.show = true;
    //   return i;
    // });
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="less" scoped>
.subsidy {
  .subsidy-header {
    padding: 20px;
    margin: 20px;
    padding-top: 60px; ;
    span {
      color: #333;
      font-size: 30px;
      font-weight: 600;
    }
  }

  .subsidy-inform {
    .demo-table-expand {
      font-size: 0;
      label {
        width: 90px;
        color: #99a9bf;
      }
      .el-form-item {
        margin-right: 0;
        margin-bottom: 10px;
        width: 100%;
      }
    }
  }

  .subsidy-Application {
    width: 600px;
    margin: 0 30px;
    padding: 0 20px 30px 0;
    span {
      display: inline-block;
      color: #666;
      font-size: 20px;
      font-weight: 400;
      margin: 20px 10px;
    }
  }
}
</style>